package com.py.credit.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.net.ssl.HttpsURLConnection;

import org.apache.log4j.Logger;
import org.apache.poi.ss.formula.functions.T;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class getEnterpriseDataUtil {
	
	private static Logger logger = Logger.getLogger(getEnterpriseDataUtil.class);
	public static String getEntInfo(String busiUrl,String parName,String parValue,Class claz) throws Exception{
		//系统级参数
		String appKey = getKey("ppKey");
		String secretKey =getKey("secretKey");
		String host ="https://api.xypy.com.cn";
		String permissionUrl = busiUrl;
		//应用级参数
		Map<String, String> inputMap = new HashMap<String, String>();
		inputMap.put("appKey", appKey);
		inputMap.put(parName, parValue);
		// 使用DigitalSign工具类加密
		String sign = DigitalSign.getDigitalSign(inputMap, secretKey);
		Map<String, String> paramMap = new HashMap<String, String>();
		DigitalSign.objectToMap(inputMap,paramMap);
		StringBuffer paramUrl = new StringBuffer();
		for(Map.Entry<String,String> map :paramMap.entrySet()){
			paramUrl.append(map.getKey()).append("=").append(URLEncoder.encode(map.getValue(),"UTF-8")).append("&");
		}
		String url = host + permissionUrl + "?" + paramUrl +  "sign=" +sign;
		logger.info("请求地址："+url);
		URL u = new URL(url);
		URLConnection urlConn = u.openConnection();
		HttpsURLConnection httpConn = (HttpsURLConnection) urlConn;
		//设置请求超时时间为5分钟
		httpConn.setConnectTimeout(300000);
		httpConn.setReadTimeout(300000);
	    httpConn.setRequestMethod("GET");
	    
		httpConn.connect();
		int responseCode=-1;
		try {
			responseCode= httpConn.getResponseCode();
		} catch (Exception e) {
			return "{\"code\":-1,\"msg\":\"请求超时\"}";
		}		
		logger.info("请求返回码："+responseCode);
		InputStream is = null;
		if (responseCode != 200) {
			is = httpConn.getErrorStream();
		} else {
			is = httpConn.getInputStream();
		}
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
		StringBuilder sb = new StringBuilder();
		String line;
		while ((line = reader.readLine()) != null) {
			sb.append(line);
		}
		
		System.out.println(sb.toString());
		return sb.toString();
	}
	
	public static List<?> jsonArrayToList(String busiUrl,String parName,String parValue,Class claz){
		String jsonStr="";
		try{
			jsonStr=getEntInfo(busiUrl,parName,parValue,claz);
		}catch(Exception e){
			e.printStackTrace();
		}
		JSONObject json=JSONObject.parseObject(jsonStr);
		JSONArray jsonArray=json.getJSONArray("data");
		if(null==jsonArray) return new ArrayList();;
		List<?> list=JSONArray.parseArray(jsonArray.toJSONString(), claz)==null?new ArrayList():JSONArray.parseArray(jsonArray.toJSONString(), claz);
		return list;
	}
	
	public static int getCount(String busiUrl,String parName,String parValue,Class claz) {
		String jsonStr="";
		try{
			jsonStr=getEntInfo(busiUrl,parName,parValue,claz);
		}catch(Exception e){
			e.printStackTrace();
		}
		logger.info(jsonStr);
		JSONObject json=JSONObject.parseObject(jsonStr);
		JSONArray jsonArray=json.getJSONArray("data");
		if(null==jsonArray) return 0;
		return jsonArray.size();
		
	}
 
	public  static String getKey(String key){
		ResourceBundle resource = ResourceBundle.getBundle("application");
		return resource.getString(key);  
	}
	/*public static void main(String[] args) {
		getKey("branch");
	}*/
}
